<?php

class Noginn_Log_Writer_Growl extends Zend_Log_Writer_Abstract
{
    /**
     * Maps logging priorities to Growl notification priority
     * @var array
     */
    protected $_priorityTypes = array(
        Zend_Log::EMERG  => Noginn_Growl::PRIORITY_EMERGENCY,
        Zend_Log::ALERT  => Noginn_Growl::PRIORITY_HIGH,
        Zend_Log::CRIT   => Noginn_Growl::PRIORITY_HIGH,
        Zend_Log::ERR    => Noginn_Growl::PRIORITY_HIGH,
        Zend_Log::WARN   => Noginn_Growl::PRIORITY_NORMAL,
        Zend_Log::NOTICE => Noginn_Growl::PRIORITY_MODERATE,
        Zend_Log::INFO   => Noginn_Growl::PRIORITY_LOW,
        Zend_Log::DEBUG  => Noginn_Growl::PRIORITY_LOW
    );
    
    /**
     * The default Growl notification priority for un-mapped priorities
     * @var string
     */    
    protected $_defaultPriorityType = Noginn_Growl::PRIORITY_NORMAL;
    
    /**
     * Maps logging priorities to Growl notification title
     * @var array
     */
    protected $_priorityTitles = array(
        Zend_Log::EMERG  => 'Emergency',
        Zend_Log::ALERT  => 'Alert',
        Zend_Log::CRIT   => 'Critical',
        Zend_Log::ERR    => 'Error',
        Zend_Log::WARN   => 'Warning',
        Zend_Log::NOTICE => 'Notice',
        Zend_Log::INFO   => 'Info',
        Zend_Log::DEBUG  => 'Debug'
    );
    
    /**
     * The default Growl notification title for un-mapped priority
     * @var string
     */
    protected $_defaultPriorityTitle = 'Log';
    
    /**
     * Flag indicating whether the log writer is enabled
     * @var boolean
     */
    protected $_enabled = true;
    
    protected $_growl;
    
    /**
     * Class constructor
     * 
     * @param Noginn_Growl $growl The instance of the Growl component to use
     */
    public function __construct(Noginn_Growl $growl)
    {
        $this->setGrowl($growl);
        
        if (php_sapi_name()=='cli') {
            $this->setEnabled(false);
        }
    }
    
    public function setGrowl($growl)
    {
        foreach ($this->_priorityTitles as $name) {
            $growl->addNotification($name);
        }
        
        $this->_growl = $growl;
    }
    
    public function getGrowl()
    {
        return $this->_growl;
    }
    
    /**
     * Enable or disable the log writer.
     * 
     * @param boolean $enabled Set to TRUE to enable the log writer 
     * @return boolean The previous value.
     */
    public function setEnabled($enabled)
    {
        $previous = $this->_enabled;
        $this->_enabled = $enabled;
        return $previous;
    }
    
    /**
     * Determine if the log writer is enabled.
     * 
     * @return boolean Returns TRUE if the log writer is enabled.
     */
    public function getEnabled()
    {
        return $this->_enabled;
    }

    /**
     * Formatting is not possible on this writer
     *
     * @return void
     */
    public function setFormatter($formatter)
    {
        require_once 'Zend/Log/Exception.php';
        throw new Zend_Log_Exception(get_class() . ' does not support formatting');
    }

    /**
     * Send a Growl notification with the log message
     *
     * @param array $event The event data
     * @return void
     */
    protected function _write($event)
    {
        if (!$this->getEnabled()) {
            return;
        }
      
        if (array_key_exists($event['priority'],$this->_priorityTypes)) {
            $type = $this->_priorityTypes[$event['priority']];
        } else {
            $type = $this->_defaultPriorityType;
        }
        
        if (array_key_exists($event['priority'],$this->_priorityTitles)) {
            $title = $this->_priorityTitles[$event['priority']];
        } else {
            $title = $this->_defaultPriorityTitle;
        }
        
        $growl = $this->getGrowl();
        $growl->notify($title, $title, $event['message'], $type);
    }
}
